#include "sphere.h"




Sphere::Sphere(ID3D10Device* d, int size, float scale)
{
	m_size=size;
	m_scale=scale;
	pd3dDevice = d;
	m_SphereObject=new DrawableObject(pd3dDevice);
	generate();
}

HRESULT Sphere::generate()
{
	//Generate vertices
	ofstream g_log("log.txt");
	SimpleVertex*vertices=GenerateSphere(m_size, m_scale, g_log);

	//Generate indices
	int ic=(m_size-1)*(m_size-1)*2*3;  //number of indices 
	DWORD * indices= (DWORD*)malloc(ic *sizeof(DWORD));

	int j=0;
	g_log<<"index buffer \n";
	for(int i=0; i<ic; i+=6)
	{
		indices[i]=j;		
		indices[i+1]=j+m_size;
		indices[i+2]=j+m_size+1;


		indices[i+3]=j;
		indices[i+4]=j+m_size+1;
		indices[i+5]=j+1;


		g_log<<indices[i]<<" ";
		g_log<<indices[i+1]<<" ";
		g_log<<indices[i+2]<<" \n";

		g_log<<indices[i+3]<<" ";
		g_log<<indices[i+4]<<" ";
		g_log<<indices[i+5]<<" \n";
		j++;
		if((j+1)%(m_size)==0) j++;
	}




	m_SphereObject->CreateVertexBuffer(vertices,m_size*m_size);
	m_SphereObject->CreateIndexBuffer(indices,ic);

	



	free(indices);
	free(vertices);

	return S_OK;
}

void Sphere::render()
{


	m_SphereObject->Draw();


}

Sphere::~Sphere()
{
	if(m_SphereObject)
	{
		delete(m_SphereObject);
		m_SphereObject=NULL;
	}


}

DrawableObject * Sphere::GetSphere()
{
	return m_SphereObject;
}

